angular.module('Controller')

.controller('WFRoleSettingCtrl', ($scope,NgTableParams,wfAlert,menuManage,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.type = $rootScope.pageType
    $scope.data=$rootScope.pageParams
    $scope.menuData=[]
    $scope.authorityData=[]
    $scope.typeList=commonConstant.roleTypeList()
    $scope.enabledList=commonConstant.enabledList()
    $scope.authorizationTypeList=commonConstant.authorizationTypeList()
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.menuLvList=commonConstant.menuLvList(true)
    loadAuthorityData=()->
        requestData = {page: 0,size: 500,accountId:$localStorage['accountId']}
        $http.get('@ostengar/role/'+$scope.data.id+'/authorities'+commonUtil.transferGetParam(requestData))
        .success (resp)->
            if resp.code == 0
                $scope.authorityData = resp.message.content;
                $scope.queryAuthorityData()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadMenuData=()->
        requestData = {page: 0,size: 500,accountId:$localStorage['accountId']}
        $http.get('@ostengar/role/'+$scope.data.id+'/modules'+commonUtil.transferGetParam(requestData))
        .success (resp)->
            if resp.code == 0
                $scope.menuData = resp.message.content;
                $scope.queryMenuData()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadAuthorityData()
    loadMenuData()
    _.extend $scope,
        newSetting:->
            $scope.data.name = ''
            $scope.data.description = ''
            $scope.data.type = 1
            $scope.data.enabled = true
        saveSetting:->
            postData={
                name:$scope.data.name
                description:$scope.data.description
                type:$scope.data.type
                enabled:$scope.data.enabled
            }
            $http.put('@ostengar/role/'+$scope.data.id,postData)
            .success((resp)->
                console.log('修改角色',resp)
                if resp.code==0
                    wfAlert.toastInfo('修改角色成功')
                else
                    wfAlert.toastError('修改角色失败：',resp.message)
            )
        selectTab:  (event, tabID)->
            $('#roleTab .active').removeClass('active')
            $('#' + tabID).addClass('active')
            $('#roleTabContent .active').removeClass('active')
            $('#' + tabID + 'Pane').addClass('active')
            return
        bindAuthority:->
            $scope.modal = $uibModal.open({
                animation: true,
                appendTo: $("#modalContent:eq(0)"),
                controller: 'WFRoleBindAuthorityCtrl',
                templateUrl: 'templates/system/role/role-bind-authority.html',
                resolve: {
                    items:  ()->{roleID:$scope.data.id,data:$scope.authorityData}
                }
            })
            $scope.modal.result.then((modalData)->
                loadAuthorityData()
            )
        bindMenu:->
            $scope.modal = $uibModal.open({
                animation: true,
                appendTo: $("#modalContent:eq(0)"),
                controller: 'WFRoleBindMenuCtrl',
                templateUrl: 'templates/system/role/role-bind-menu.html',
                resolve: {
                    items:  ()->{roleID:$scope.data.id,data:$scope.menuData}
                }
            })
            $scope.modal.result.then((modalData)->
                loadMenuData()
            )
        changeMenu:->
            console.log($scope.menus)
        queryAuthorityData: (firstPage)->
            if(firstPage == true)
                $scope.authorityTableParams.page(1)
            $scope.authorityTableParams.reload()
        queryMenuData: (firstPage)->
            if(firstPage == true)
                $scope.menuTableParams.page(1)
            $scope.menuTableParams.reload()
        changeAuthority:->
            putData={
                accountId:2
                authority:$scope.data.authority
                description:$scope.data.description
                enabled:$scope.data.enabled
                type:$scope.data.type
            }
            getParams=commonUtil.transferGetParam(putData)
            $http.put('@ostengar/authority/'+$scope.data.id,putData)
            .success((resp)->
                if(resp.code==0)
                    wfAlert.toastInfo('修改权限成功')
                else
                    wfAlert.toastError('修改权限失败：',resp.message)
            )
        removeMenu:(item)->
            $http.delete('@ostengar/role/'+$scope.data.id+'/modules?modules='+item.id)
            .success((resp)->
                if(resp.code==0)
                    wfAlert.toastInfo('移除角色菜单成功')
                    loadMenuData()
                else
                    wfAlert.toastError('移除角色菜单成功')
            )
        removeAuthority:(item)->
            $http.delete('@ostengar/role/'+$scope.data.id+'/authorities?authorities='+item.id)
            .success((resp)->
                if(resp.code==0)
                    wfAlert.toastInfo('移除角色权限成功')
                    loadAuthorityData()
                else
                    wfAlert.toastError('移除角色权限成功')
            )
    $scope.authorityTableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: commonConstant.tableCounts,
                getData: ($defer, params)->
#                    $scope.doQueryAuthority($defer,params)
                    $defer.resolve($scope.authorityData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
                    params.total($scope.authorityData.length)
                    return ;
            }
    )
    $scope.menuTableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: commonConstant.tableCounts,
                getData: ($defer, params)->
#                    $scope.doQueryMenu($defer,params)
                    $defer.resolve($scope.menuData.slice((params.page() - 1) * params.count(), params.page() * params.count()))
                    params.total($scope.menuData.length)
                    return ;
            }
    )
)